package com.java.service.impI;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.dto.Result;
import com.java.entity.User;
import com.java.mapper.UserMapper;
import com.java.service.IUserService;
import com.java.utils.RegexUtils;
import com.java.utils.UserIdMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author 李启旋
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public Result getUserList(String key,Integer pageNum, Integer size) {
        List<User> userList;
        int total;
        if (key == null || key.isEmpty()){
            total = query().eq("is_delete",-1).count();
            userList = query().eq("is_delete",-1).orderByAsc("id").page(new Page<>(pageNum, size)).getRecords();
        }else{
            total=query().like("name", key).count();
            userList = query().eq("is_delete",-1).like("name", key).orderByAsc("id").page(new Page<>(pageNum, size)).getRecords();
        }
        if (userList == null || userList.isEmpty()){
            return Result.ok("数据库没有数据");
        }
        return Result.ok(userList,total);
    }

    @Override
    public Result saveUser(User user) {
        if(user.getName() == null ||user.getName().isEmpty() ){
            return Result.ok("姓名不能为空！！！");
        }
        if(RegexUtils.isPhoneInvalid(user.getPhone())){
            return Result.ok("手机号格式不正确！！！");
        }
        user.setIs_delete(-1);
        boolean saveUser = save(user);
        if (BooleanUtil.isTrue(saveUser)){
            return Result.ok("保存成功");
        }
        return Result.ok("保存失败");
    }

    @Override
    public Result updateUser(Integer id,User user) {
        if(user.getName() == null ||user.getName().isEmpty() ){
            return Result.ok("姓名不能为空！！！");
        }
        if(RegexUtils.isPhoneInvalid(user.getPhone())){
            return Result.ok("手机号格式不正确！！！");
        }
        boolean updateUser = update().eq("id", id).update(user);
        if (BooleanUtil.isTrue(updateUser)){
            return Result.ok("修改成功");
        }
        return Result.ok("修改失败");
    }

    @Override
    public Result deleteUserById(Integer id) {
        User user = userMapper.selectById(id);
        if (user == null){
            return Result.ok("未查到Id");
        }
        UserIdMap.saveUserId(id);
        List<Integer> list = Arrays.asList(id);
        userMapper.updateIsDelete(list);
        return Result.ok("删除成功");
    }

    @Override
    public Result rollback() {
        List<Integer> list = UserIdMap.getUserMax();
        if (list == null){
            return Result.ok("没有可以回滚的了");
        }
        userMapper.updateIsDelete(list);
        UserIdMap.removeUserMax();
        return Result.ok("回滚成功");
    }

    @Override
    public Result deleteUserList(List<Integer> list) {
        userMapper.updateIsDelete(list);
        UserIdMap.saveList(list);
        return Result.ok("批量删除成功");
    }
}
